%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% MAIN FILE

% "DEFORESTATION IN THE AMAZON:
% A UNIFIED FRAMEWORK FOR ESTIMATION AND POLICY ANALYSIS"

% by Eduardo Souza-Rodrigues

% This version: November 2018

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% LAND-USE MODEL:

% ln(Y/(1-Y)) = TC'alpha(U) + X'beta(U) + beta_cons(U)

% where:
% Y     = share of deforested area
% TC    = transportation cost to the nearest port
% X     = covariates
% U     = municipality-level unobservable 
% D     = [D1 D2], straight-line distances to: 
%         (i) nearest port and (ii) nearest capital,
%         D is the IV for TC
% alpha = parameter of interest
% beta  = finite dimensional parameter (covariates)

% Moment Restriction:
% E[ 1{ln(Y/(1-Y)) - TC'alpha(u) - X'beta(u) - beta_cons(u) < 0} - u | D, X] = 0

% Estimation methods: Chernozhukov and Hansen's IVQR

% Unit of observation: municipalitites in the Brazilian Amazon

% There are 4 groups of farm sizes:
% farm = 1, if farm size < 5   has                 (small farms)
% farm = 2, if farm size > 5   has and  < 50  has  (small-medium farms)
% farm = 3, if farm size > 50  has and  < 500 has  (medium-large farms)
% farm = 4, if farm size > 500 has                 (large farms)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% (A) CLEAR WORKSPACE AND CALL TOOLBOXES

% Clear workspace
clc
clear all

% Current Folder
current_folder = pwd;

% Folder with the Data Sets (may choose a different folder)
data_folder = current_folder;


%% (B) SET UP PARAMETERS AND MODEL SPECIFICATION 

% I. Define Global Variables

global model farm prod_index tau t ts ts_length myzero critical penalty d_iv cluster_bs prop
global est_spqiv results_est results_plot results_app_median results_yields_table...
    results_yields_plot results_est_spqiv results_plot_spqiv results_bs results_sat results_app_ls


% II. Select Set of Results to Show

% Indicators
results_est          = 1;     % Show main results in the paper (tables)
results_plot         = 0;     % Show main figures in the paper 
results_app_median   = 0;     % Appendix: Show Median Land-use Regressions       
results_bs           = 1;     % Appendix: Show (and compute) cluster bootstrap standard errors 
results_app_ls       = 0;     % Appendix: Show (and compute) lost surplus when fines set at pre-2004 levels
results_est_spqiv    = 0;     % Appendix: Show results from SPQIV estimation     
results_plot_spqiv   = 0;     % Appendix: Show plots from SPQIV estimation       
results_yields_table = 0;     % Appendix: Show results for Yields Regressions    
results_sat          = 0;     % Appendix: Show satellite-census regression       
results_yields_plot  = 0;     % Appendix: Show figure for 'Fixed-Proportions' vs 'Vertical' Models
% If results = 0, do not show results
% If results = 1, show results


% III. Select Model Specification, Estimation Strategy, and Counterfactual Demand

% Set of Covariates:
model = 0;
% model = 0, Main Specification
% model = 1, no population (appendix)
% model = 2, no land title proxy (appendix)
% model = 3, no distance to ibama, nor fines (appendix)
% model = 4, no distance to ibama (appendix)
% model = 5, no fines (appendix)
% model = 6, no spatially lagged regressors (appendix)

% Set of Instruments:
d_iv = 0;
% d_iv = 0, if use both dist_port and dist_cap as IV -- Main Specification
% d_iv = 1, if use only dist_port as IV -- Appendix

% Estimate the Semiparametric Model (SPQIV), Appendix
est_spqiv = 0;
% est_spqiv = 0, do not estimate SPQIV
% est_spqiv = 1, estimate SPQIV


% IV. Set up some parameters for the regressions

% Quantiles (used in the estimation procedure)
tau = 0.05:0.01:0.95;
t   = length(tau);

% Selected Quantiles (to display results)
[~,ts1]   = min(abs(tau-0.10));
[~,ts2]   = min(abs(tau-0.25));
[~,ts3]   = min(abs(tau-0.50));
[~,ts4]   = min(abs(tau-0.75));
[~,ts5]   = min(abs(tau-0.90));
ts        = [ts1 ts2 ts3 ts4 ts5];
tss       = [10 25 50 75 90];
ts_length = length(ts);
clear ts1 ts2 ts3 ts4 ts5

% Truncate variables when necessary using "myzero"
myzero = 1e-5;

% Critical Value for Confidence Intervals
critical = norminv(0.975);  % 95%-CI
%critical = norminv(0.995); % 99%-CI
%critical = norminv(0.95);  % 90%-CI

% Share of agricultural land allowed in private land (used to compue farmer's lost surplus) 
prop = 0.2;


%% (C) RUN MAIN REGRESSIONS, COMPUTE THE DEMAND FOR DEFORESTATION, AND THE SUPPLY OF AVOIDED EMISSIONS

tic;

for farm = 1:4
    
    % Small Farms
    if farm == 1
    
        % (a) Load the Data Set
        data1 = [data_folder,'\land_use_matlab_small.txt'];
        load(data1);
        data = land_use_matlab_small;
        clear data1 land_use_matlab_small;

        % (b) Run Land-Use Regressions and Save Results: OLS, 2SLS, QR, IVQR
        run dem_def_reg;

        % (c) Generate Figure: Predicted Share of Deforestation vs TC
        if results_plot == 1
            run dem_def_plot_predict_vs_tc;
        end      
        
        % (d) Compute Demand for Deforestation and Avoided Carbon Emissions
        % Fix Productivity Index        
        for prod_index = 1:2
            
            % Fix Monitoring
            for penalty = 0:1
                
                % Main Specification
                if prod_index == 2 && penalty == 0  % Crops-Pasture Index, Actual Monitoring
                    
                    % Use Crops & Pasture Index ('Fixed-Proportions' Model)
                    Qa = Qa2;   
                    
                    % Compute Demand for Deforestation and Save Results
                    run dem_def_compute_demand;
                    
                    % Compute Avoided Emissions of CO2 and Save Results
                    run dem_def_carbon_emissions;               
                    
                % Monitoring -- Robustness    
                elseif prod_index == 2 && penalty == 1  % Crops-Pasture Index, Set Fines at pre-2004 levels
                    
                    % Use Crops & Pasture Index
                    Qa = Qa2;   
                    
                    % Compute Demand for Deforestation and Save Results
                    run dem_def_compute_demand;
                    
                    % Compute Avoided Emissions of CO2 and Save Results
                    run dem_def_carbon_emissions;                                   
                
                % Productivity Index -- Appendix
                elseif prod_index == 1 && penalty == 0  % Only-Crops Index, Actual Monitoring
                    
                    % Use Only Crops ('Vertical' Model)
                    Qa = Qa1;   
                    
                    % Compute Demand for Deforestation and Save Results
                    run dem_def_compute_demand;                 
                                                   
                end
            end
        end
        
        % (e) Run Land-Use Semiparametric Model -- No need to estimate SPQIV for small farms
        %if est_spqiv == 1
        %    run dem_def_spqiv;
        %end
                    
                
    % Small-Medium Farms
    elseif farm == 2    

        % (a) Load the Data Set
        data1 = [data_folder,'\land_use_matlab_small_medium.txt'];
        load(data1);
        data = land_use_matlab_small_medium;
        clear data1 land_use_matlab_small_medium;

        % (b) Run Land-Use Regressions and Save Results: OLS, 2SLS, QR, IVQR
        run dem_def_reg;

        % (c) Generate Figure: Predicted Deforestation vs TC
        if results_plot == 1
            run dem_def_plot_predict_vs_tc;
        end
        
        % (d) Compute Demand for Deforestation and Avoided Carbon Emissions
        % Fix Productivity Index
        for prod_index = 1:2
            
            % Fix Monitoring
            for penalty = 0:1
                
                % Main Specification
                if prod_index == 2 && penalty == 0  % Crops-Pasture Index, Actual Monitoring
                    
                    % Use Crops & Pasture Index ('Fixed-Proportions' Model)
                    Qa = Qa2;   
                    
                    % Compute Demand for Deforestation and Save Results
                    run dem_def_compute_demand;
                    
                    % Compute Avoided Emissions of CO2 and Save Results
                    run dem_def_carbon_emissions;               
                    
                % Monitoring -- Robustness    
                elseif prod_index == 2 && penalty == 1  % Crops-Pasture Index, Set Fines at pre-2004 levels
                    
                    % Use Crops & Pasture Index
                    Qa = Qa2;   
                    
                    % Compute Demand for Deforestation and Save Results
                    run dem_def_compute_demand;
                    
                    % Compute Avoided Emissions of CO2 and Save Results
                    run dem_def_carbon_emissions;                                   
                
                % Productivity Index -- Appendix
                elseif prod_index == 1 && penalty == 0  % Only-Crops Index, Actual Monitoring
                    
                    % Use Only Crops ('Vertical' Model)
                    Qa = Qa1;   
                    
                    % Compute Demand for Deforestation and Save Results
                    run dem_def_compute_demand;                 
                                                       
                end
            end
        end
        
        % (e) Run Land-Use Semiparametric Model
        if est_spqiv == 1
            run dem_def_spqiv;
        end
        
        
    % Medium-Large Farms
    elseif farm == 3

        % (a) Load the Data Set
        data1 = [data_folder,'\land_use_matlab_medium_large.txt'];
        load(data1);
        data = land_use_matlab_medium_large;
        clear data1 land_use_matlab_medium_large;

        % (b) Run Land-Use Regressions and Save Results: OLS, 2SLS, QR, IVQR
        run dem_def_reg;

        % (c) Generate Figure: Predicted Deforestation vs TC
        if results_plot == 1
            run dem_def_plot_predict_vs_tc;
        end
        
        % (d) Compute Demand for Deforestation and Avoided Carbon Emissions
        % Fix Productivity Index
        for prod_index = 1:2
            
            % Fix Monitoring
            for penalty = 0:1
                
                % Main Specification
                if prod_index == 2 && penalty == 0  % Crops-Pasture Index, Actual Monitoring
                    
                    % Use Crops & Pasture Index ('Fixed-Proportions' Model)
                    Qa = Qa2;   
                    
                    % Compute Demand for Deforestation and Save Results
                    run dem_def_compute_demand;
                    
                    % Compute Avoided Emissions of CO2 and Save Results
                    run dem_def_carbon_emissions;               
                    
                % Monitoring -- Robustness    
                elseif prod_index == 2 && penalty == 1  % Crops-Pasture Index, Set Fines at pre-2004 levels
                    
                    % Use Crops & Pasture Index
                    Qa = Qa2;   
                    
                    % Compute Demand for Deforestation and Save Results
                    run dem_def_compute_demand;
                    
                    % Compute Avoided Emissions of CO2 and Save Results
                    run dem_def_carbon_emissions;                                   
                
                % Productivity Index -- Appendix
                elseif prod_index == 1 && penalty == 0  % Only-Crops Index, Actual Monitoring
                    
                    % Use Only Crops ('Vertical' Model)
                    Qa = Qa1;   
                    
                    % Compute Demand for Deforestation and Save Results
                    run dem_def_compute_demand;                 
                                                       
                end
            end
        end
        
        % (e) Run Land-Use Semiparametric Model
        if est_spqiv == 1
            run dem_def_spqiv;
        end
        
        
    % Large Farms        
    elseif farm == 4

        % Load the Data Set
        data1 = [data_folder,'\land_use_matlab_large.txt'];
        load(data1);
        data = land_use_matlab_large;
        clear data1 land_use_matlab_large;

        % (b) Run Land-Use Regressions and Save Results: OLS, 2SLS, QR, IVQR
        run dem_def_reg;

        % (c) Generate Figure: Predicted Deforestation vs TC
        if results_plot == 1
            run dem_def_plot_predict_vs_tc;
        end
        
        % (d) Compute Demand for Deforestation and Avoided Carbon Emissions
        % Fix Productivity Index
        for prod_index = 1:2
            
            % Fix Monitoring
            for penalty = 0:1
                
                % Main Specification
                if prod_index == 2 && penalty == 0  % Crops-Pasture Index, Actual Monitoring
                    
                    % Use Crops & Pasture Index ('Fixed-Proportions' Model)
                    Qa = Qa2;   
                    
                    % Compute Demand for Deforestation and Save Results
                    run dem_def_compute_demand;
                    
                    % Compute Avoided Emissions of CO2 and Save Results
                    run dem_def_carbon_emissions;               
                    
                % Monitoring -- Robustness    
                elseif prod_index == 2 && penalty == 1  % Crops-Pasture Index, Set Fines at pre-2004 levels
                    
                    % Use Crops & Pasture Index
                    Qa = Qa2;   
                    
                    % Compute Demand for Deforestation and Save Results
                    run dem_def_compute_demand;
                    
                    % Compute Avoided Emissions of CO2 and Save Results
                    run dem_def_carbon_emissions;                                   
                
                % Productivity Index -- Appendix
                elseif prod_index == 1 && penalty == 0  % Only-Crops Index, Actual Monitoring
                    
                    % Use Only Crops ('Vertical' Model)
                    Qa = Qa1;   
                    
                    % Compute Demand for Deforestation and Save Results
                    run dem_def_compute_demand;                 
                                   
                end
            end
        end
        
        % (e) Run Land-Use Semiparametric Model
        if est_spqiv == 1
            run dem_def_spqiv;
        end        
        
        
    end
    
end

% Time Elapsed
tElapsed_total = toc;
disp('----------------------------------------------------------------------------')
display(' ')
display('Time Elapsed (minutes)')
tElapsed_total/60
clear tElapsed_total ans


%% (C) SHOW MAIN RESULTS -- TABLES

if results_est == 1
    
    disp('----------------------------------------------------------------------------')
    disp('----------------------------------------------------------------------------')
    
    % 1. Present Model Specification
    if model == 0
        
        % Main Specification
        disp('MODEL SPECIFICATION: MAIN SPECIFICATION')
        
    elseif model == 1
        
        % No Population
        disp('MODEL SPECIFICATION: ALL COVARIATES, BUT NO POPULATION')
        
    elseif model == 2
                
        % No land title proxy
        disp('MODEL SPECIFICATION: ALL COVARIATES, BUT NO LAND TENURE PROXY')
        
    elseif model == 3
        
        % No distance to ibama, nor fines
        disp('MODEL SPECIFICATION: ALL COVARIATES, BUT NO DISTANCE TO IBAMA, NOR FINES')
    
    elseif model == 4
        
        % No distance to ibama
        disp('MODEL SPECIFICATION: ALL COVARIATES, BUT NO DISTANCE TO IBAMA, NOR FINES')
    
    elseif model == 5
        
        % No Fines
        disp('MODEL SPECIFICATION: ALL COVARIATES, BUT NO FINES')
    
    elseif model == 6
        
        % No Spatially Lagged Regressors
        disp('MODEL SPECIFICATION: ALL COVARIATES, BUT NO SPATIALLY LAGGED REGRESSORS')
        
    end
    
    % Set of Instruments
    if d_iv == 0
        
        % Instruments
        disp('INSTRUMENTS: STRAIGHT-LINE DISTANCES TO PORT AND CAPITAL (MAIN SPECIFICATION)')
        
    elseif d_iv == 1
        
        % Instruments
        disp('INSTRUMENTS: STRAIGHT-LINE DISTANCE TO PORT ONLY')
    
    end    
    
    % 2. First-Stage Regression: Table 3 in the Paper
    run dem_def_first_stage_reg;

    % 3. Land Use Regressions and Marginal Effects, by Farm Size: Tables 4 and 5 in the Paper
    run dem_def_reg_tables;
    
    % 4. Compute Lost Surpluses, and show results
    penalty = 0;    % Set Actual Monitoring
    run dem_def_lost_surplus;
    pause

end

%% (D) SHOW MAIN RESULTS -- FIGURES

if results_plot == 1
    
    % 1. Plot Predicted Deforestation vs Transportation Costs, by Farm Size (Figure 3 in the Paper)
    openfig('quantile_small.fig','new','visible'); pause
    openfig('quantile_smallmedium.fig','new','visible'); pause
    openfig('quantile_mediumlarge.fig','new','visible'); pause
    openfig('quantile_large.fig','new','visible'); pause
    
    % 2. Plot Demand for Deforestation, Total and by Farm Size (Figure 4 in the Paper)
    run dem_def_plot_demand;

    % 3. Plot Demand for Deforestation, Actual Monitoring vs Fines at Pre-2004 Levels (Figure 6 in the Paper)    
    run dem_def_plot_demand_monitoring;
    
    % 4. Plot Supply of Avoided Emissions of Carbon (Figure 7 in the Paper)
    run dem_def_plot_emissions;

end


%% (F) SUPPLEMENTAL MATERIAL

% 1. IVQR Median Regression -- Show coefficients on all regressors 
% Can generate Tables 1, 2, 3, 4, and 6 of the Supplement Document
% based on various model specifications (for model = 0, 1, 2, 3, 4, 5, 6)
if results_app_median == 1
    
    % Generate Median Regressions Tables
    run dem_def_median_reg_tables_app;
    
end

% 2. Compute and Show Lost Surpluses when fines are set at pre-2004 levels
% Generate numbers in Table 5 of the Supplement Document
if results_app_ls == 1
    
    % Compute Lost Surpluses, and show results
    penalty = 1;    % Set Fines at pre-2004 levels
    disp('----------------------------------------------------------------------------')
    disp('APPENDIX: LOST SURPLUS, FINES SET AT PRE-2004 LEVELS')
    run dem_def_lost_surplus;
    
    % Compute Avoided Emissions, and show results    
    run dem_def_emissions_monitoring;
    
end

% 3. Geographic Cluster Bootstrap
% Generate standard errors presented in Table 7 of the Supplement Document
if results_bs == 1   
    
    % Implement Pair Cluster Bootstrap
    run cluster_bootstrap.m
    
end

% 4. SPQIV Model
% Generate Figure 2 and Table 10 of the Supplement Document
if est_spqiv == 1 && results_est_spqiv == 1
    
    % Generate Median SPQIV Regressions Tables
    run dem_def_median_spqiv_tables_app;
    
end
    
if est_spqiv == 1 && results_plot_spqiv == 1
    
    % Figures
    openfig('spqiv_smallmedium.fig','new','visible'); pause
    openfig('spqiv_mediumlarge.fig','new','visible'); pause
    openfig('spqiv_large.fig','new','visible'); pause
    
end


% 5. Yields Regressions 
% Generate Tables 11 and 12 of the Supplement Document
if results_yields_table == 1
    
    for farm = 1:4
        
        % Run Yields Regressions
        run dem_def_reg_yields;
            
    end
    
end

% 6. Satellite vs Census Deforestation Regressions
% Generate Table 13 of the Supplement Document
if results_sat == 1
    
    % Run Regressions
    run dem_def_sat_reg.m
    
end


% 7. Plot Demand for Deforestation, 'Fixed-Proportions' vs 'Vertical' Models
% Generate Figure 3 of the Supplement Document
if results_yields_plot == 1
    
    % Plot Demand Curves, 'Fixed-Proportions' vs 'Vertical' Models
    run dem_def_plot_fixedprop_vs_vertical_demand.m; 
    
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

